﻿<?php
/**
 * Forum Controller
 *
 * Sewiki is a experimental software for course
 * Software Engineering in HIT and it's devoloped
 * upon CodeIgniter Framework. More infomation of
 * the framework, please vist http://codeigniter.com/
 *
 * @package  : CodeIgniter
 * @category : controller/forum
 * @author   : Oneplus <oneplus.lau@gmail.com>
 * @data     : 2011-06-14
 */
class Forum extends CI_Controller {
	/**
	 * The constructor
	 *
	 * @access : public
	 * @return : void
	 */
	function __construct() {
		parent::__construct();
		$this->load->database();
		$this->load->model('user');
		$this->load->model('poster');
		$this->load->model('thread');
	}
	
	/**
	 * Method Post Thread
	 *
	 * Main controller method for user create a thread
	 *
	 * @access : public
	 * @param  : int, cid
	 * @return : void
	 */
	public function postThread($cid) {
		$this->load->view('forum/createThreadForm');
		
		if($this->input->post('submit')) {
			$title   = $this->input->post('title');
			$content = $this->input->post('content');
			$uid     = $this->session->userdata('uid');
			
			$data = array(
				'title'       => $title,
				'content'     => $content,
				'create_time' => time(),
				'cid'         => $cid,
				'uid'         => $uid);
				
			$this->thread->createThread($data);
		}
	}
	
	/**
	 * Method Post Poster
	 *
	 * Main controller method for user create a poster
	 *
	 * @access : public
	 * @param  : int, tid
	 * @return : void
	 */
	public function postPoster($tid) {
		$this->load->view('forum/createPosterForm');
		
		if($this->input->post('submit')) {
			$content = $this->input->post('content');
			$uid     = $this->session->userdata('uid');
			
			$data = array(
				'content'     => $content, 
				'create_time' => time(),
				'uid'         => $uid,
				'tid'         => $tid);
				
			$this->poster->createPoster($data);
		}
	}
	
	/**
	 * Method Delete Poster
	 *
	 * Main controller method for user delete a poster
	 *
	 * @access : public
	 * @param  : int, pid
	 * @return : void
	 */
	public function deletePoster($pid) {
		$this->poster->deletePoster($pid);
	}
	
	/**
	 * Method Delete Thread
	 *
	 * Main controller method for user delete a thread
	 * and all the posters reply to it
	 *
	 * @access : public
	 * @param  : int, tid
	 * @return : void
	 */
	public function deleteThread($tid) {
		$posters = $this->db->get_where('posters', array('tid' => $tid));
		
		foreach ($posters->result() as $poster) {
			$pid = $poster->pid;
			$this->poster->deletePoster($pid);
		}
		
		$this->thread->deleteThread($tid);
	}
	
	public function browseThread($cid) {
		$threads = $this->db->get_where('threads', array('cid' => $cid));
		
		foreach ($threads->result() as $thread) {
			$uid      = $thread->uid;
			$user     = $this->db->get_where('users', array('uid' => $uid));
			$username = $user->row()->username;

			$data = array(
				'username'    => $username, 
				'title'       => $thread->title,
				'create_time' => $thread->create_time);
				
			$this->load->view('forum/threadItem', $data);
		}
	}
	
	public function browsePoster($tid) {
		$thread   = $this->db->get_where('threads', array('tid' => $tid));
		$uid      = $thread->row()->uid;
		$user     = $this->db->get_where('users', array('uid' => $uid));
		$username = $user->row()->username;
		
		$data = array(
			'title'    => $thread->row()->title,
			'content'  => $thread->row()->content,
			'username' => $username);
		
		$this->load->view('forum/thread');
		
		$posters = $this->db->get_where('posters', array('pid' => $pid));
		foreach ($posters->result() as $poster) {
			$uid  = $poster->row()->uid;
			$user = $this->db->get_where('users', array('uid' => $uid));
			$username = $user->row()->username;
			
			$data = array(
				'content'  => $poster->content,
				'username' => $username);
				
			$this->load->view('forum/posterItem');
		}
 	}

	/**
	 * Method Thread
	 *
	 * Main controller method for user's operation in thread page
	 *
	 * @access : public
	 * @param  : int, tid
	 * @return : void
	 */
	public function thread($cid) {
		$this->db->order_by('last_reply desc');
		$threads         = $this->db->get_where('threads', array('cid' => $cid));
		$data            = array();
		$data['threads'] = array();
		
		foreach ($threads->result() as $thread) {
			$uid      = $thread->uid;
			$tid      = $thread->tid;
			$user     = $this->db->get_where('users', array('uid' => $uid));
			$username = $user->row()->username;

			$thread_info = array(
				'username'    => $username, 
				'title'       => $thread->title,
				'create_time' => $thread->create_time,
				'detail_url'  => site_url('forum/Forum/poster/' . strval($tid)));
				
			if(($this->user->isOwnerOfThread($this->session->userdata('uid'), $tid) == true)
				|| ($this->user->isAdminForThread($this->session->userdata('uid'), $tid) == true)) {
				$thread_info['operators'] = array(
					array(
						'op_url'  => site_url('forum/Forum/deleteThread/' . strval($tid)),
						'op_name' => '删除主题'));
			}
			else {
				$thread_info['operators'] = array();
			}
				
			array_push($data['threads'], $thread_info);
		}
		$this->load->view('forum/threadList', $data);
		
		if($this->input->post('submit')) {
			$title   = $this->input->post('title');
			$content = $this->input->post('content');
			$uid     = $this->session->userdata('uid');
			
			$data = array(
				'title'       => $title,
				'content'     => $content, 
				'create_time' => date("Y-m-d H:i:s", time()),
				'uid'         => $uid,
				'cid'         => $cid);
				
			$this->thread->createThread($data);
			
			redirect('forum/Forum/thread/' . strval($cid), 'refresh');
		}
	}

	/**
	 * Method Poster
	 *
	 * Main controller method for user's operation in poster page
	 *
	 * @access : public
	 * @param  : int, tid
	 * @return : void
	 */
	public function poster($tid) {
		$thread          = $this->thread->getThread($tid);
		if($this->user->isOwnerOfThread($this->session->userdata('uid'), $tid) == true) {
			$thread['operators'] = array(array(
				'op_url'  => site_url('forum/Forum/deleteThread/' . strval($tid)),
				'op_name' => '删除主题'));
		}
		else {
			$thread['operators'] = array();
		}
		$posters         = $this->db->get_where('posters', array('tid' => $tid));
		$data            = array();
		$data['posters'] = array();
		$data['thread']  = $thread;
		
		foreach ($posters->result() as $poster) {
			$uid      = $poster->uid;
			$pid      = $poster->pid;
			$user     = $this->user->getUser($uid);
			$username = $user['username'];
			
			$poster_info = array(
				'username'    => $username,
				'content'     => $poster->content,
				'create_time' => $poster->create_time);
				
			if($this->user->isOwnerOfPoster($this->session->userdata('uid'), $pid) == true
				|| $this->user->isAdminForPoster($this->session->userdata('uid'), $pid) == true) {
				$poster_info['operators'] = array(
					array(
						'op_url'  => site_url('forum/Forum/deletePoster/' . strval($pid)),
						'op_name' => '删除回复'));
			}
			else {
				$poster_info['operators'] = array();
			}
			
			array_push($data['posters'], $poster_info);
		}
		$this->load->view('forum/posterList', $data);
		
		if($this->input->post('submit')) {
			$content = $this->input->post('content');
			$uid     = $this->session->userdata('uid');
			
			$data = array(
				'content'     => $content, 
				'create_time' => date("Y-m-d H:i:s", time()),
				'uid'         => $uid,
				'tid'         => $tid);
				
			$this->poster->createPoster($data);
			$this->thread->touch($tid);
			
			redirect('forum/Forum/poster/' . strval($tid), 'refresh');
		}
	}
}
?>